package com.ruoyi.web.controller.teacher;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.teacher.domain.SysStudent;
import com.ruoyi.teacher.service.ISysClassesService;
import com.ruoyi.teacher.service.ISysCollegeService;
import com.ruoyi.teacher.service.ISysStudentService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * sysStudentController
 * 
 * @author ruoyi
 * @date 2022-01-13
 */
@Controller
@RequestMapping("/teacher/student")
public class SysStudentController extends BaseController
{
    private String prefix = "teacher/student";

    @Autowired
    private ISysStudentService sysStudentService;
    @Autowired
    private ISysCollegeService sysCollegeService;
    @Autowired
    private ISysClassesService sysClassesService;

    @RequiresPermissions("teacher:student:view")
    @GetMapping()
    public String student()
    {
        return prefix + "/student";
    }

    /**
     * 查询sysStudent列表
     */
    @RequiresPermissions("teacher:student:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysStudent sysStudent)
    {
        startPage();
        List<SysStudent> list = sysStudentService.selectSysStudentList(sysStudent);
        return getDataTable(list);
    }

    /**
     * 导出sysStudent列表
     */
    @RequiresPermissions("teacher:student:export")
    @Log(title = "sysStudent", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysStudent sysStudent)
    {
        List<SysStudent> list = sysStudentService.selectSysStudentList(sysStudent);
        ExcelUtil<SysStudent> util = new ExcelUtil<SysStudent>(SysStudent.class);
        return util.exportExcel(list, "学生数据");
    }

    @RequiresPermissions("teacher:student:view")
    @GetMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate()
    {
        ExcelUtil<SysStudent> util = new ExcelUtil<SysStudent>(SysStudent.class);
        return util.importTemplateExcel("学生数据");
    }

    @Log(title = "学生管理", businessType = BusinessType.IMPORT)
    @RequiresPermissions("teacher:student:import")
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<SysStudent> util = new ExcelUtil<SysStudent>(SysStudent.class);
        List<SysStudent> userList = util.importExcel(file.getInputStream());
        String message = sysStudentService.importStudent(userList, updateSupport, getLoginName());
        return AjaxResult.success(message);
    }

    /**
     * 新增sysStudent
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        mmap.put("colleges", sysCollegeService.selectCollegeAll());
        mmap.put("classz", sysClassesService.selectClassesAll());
        return prefix + "/add";
    }

    /**
     * 新增保存sysStudent
     */
    @RequiresPermissions("teacher:student:add")
    @Log(title = "sysStudent", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SysStudent sysStudent)
    {
        sysStudent.setCreateBy(getLoginName());
        return toAjax(sysStudentService.insertSysStudent(sysStudent));
    }

    /**
     * 修改sysStudent
     */
    @RequiresPermissions("teacher:student:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        SysStudent sysStudent = sysStudentService.selectSysStudentById(id);
        mmap.put("colleges", sysCollegeService.selectCollegesByCollegeId(sysStudent.getCollegeId()));
        mmap.put("classz", sysClassesService.selectClassesByClassesId(sysStudent.getClassesId()));
        mmap.put("sysStudent", sysStudent);
        return prefix + "/edit";
    }

    /**
     * 修改保存sysStudent
     */
    @RequiresPermissions("teacher:student:edit")
    @Log(title = "sysStudent", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SysStudent sysStudent)
    {
        sysStudent.setUpdateBy(getLoginName());
        return toAjax(sysStudentService.updateSysStudent(sysStudent));
    }

    /**
     * 删除sysStudent
     */
    @RequiresPermissions("teacher:student:remove")
    @Log(title = "sysStudent", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(sysStudentService.deleteSysStudentByIds(ids));
    }


    /**
     * 校验用户名
     */
    @PostMapping("/checkCardUnique")
    @ResponseBody
    public String checkCardUnique(SysStudent sysStudent)
    {
        return sysStudentService.checkCardUnique(sysStudent.getCard());
    }

    @PostMapping("/checkStudentIdUnique")
    @ResponseBody
    public String checkStudentIdUnique(SysStudent sysStudent)
    {
        return sysStudentService.checkStudentIdUnique(sysStudent.getStudentId());
    }


}
